package cn.fenqing168.springcloud.service.impl;

import cn.fenqing168.springcloud.service.IMessageProviderService;
import cn.hutool.core.map.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

import java.util.HashMap;
import java.util.Map;

/**
 * 发送消息的实现类
 */
@EnableBinding(Source.class)
public class MessgaeProviderServiceImpl implements IMessageProviderService {

    /**
     * 这里beanname 与 yml配置的bindings中的key 对应
     */
    @Autowired
    @Qualifier("output")
    private MessageChannel messageChannel;

    @Override
    public String send() {
        String content = "hello world";
        Map<String, Object> data = MapUtil.builder(new HashMap<String, Object>())
                .put("message", content).build();
        messageChannel.send(MessageBuilder.withPayload(data).setHeader("content-type", "applicaton/json").build());
        return content;
    }
}
